/**
 * Created by ZFL on 2018/3/7.
 */
function PageInit(){

    //发送验证码
    $(".but").click(function () {
        sendCode()
    });
    //绑定手机号
    $('.but-login').click(function(){
        bindPhone();
    })
}

function bindPhone() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.mobile =$("#phone-id").val();
            obj.validCode =$("#code-id").val();
            obj.undefinedId = $.getStorage('xmOpenid');
            obj.undefinedType =2; //第三方登录类型1APP 2H5
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        apiUrl:$.appApi(),
        api: 'yg_baijiaapp/unDefinedUser/getUndefinedUserAdd',
        success: function (result) {
            if (result.code == 10000){
                //YDUI.dialog.toast('注册成功','success',1000);
                saveInfo(result.data,'绑定成功');
                $('.bindPhone').hide();
                $('.publicSign').show();
            } else {
                YDUI.dialog.toast(result.message,'error',1000);
            }
        }
    });
}

//发送短信验证码
function sendCode() {
    if ($("#phone-id").val() == ""){
        YDUI.dialog.toast("请先输入手机号码",'error',1000);
    } else if ($("#phone-id").val().length != 11) {
        YDUI.dialog.toast("手机号码格式不正确",'error',1000);
    } else {
        $("#send-code-id").attr("disabled","disabled");
        $.ajaxHandler({
                model: function () {
                    var obj = {};
                    obj.mobile = $("#phone-id").val();
                    obj.type = 1;
                    console.log(obj);
                    return obj;
                },
                visibleTime: 0,
                dataType: "json",
                api: 'yg_baijiaapp/CheckCode/sendVerificationCode',
                success: function (result) {
                    console.log(result);
                    if (result.code == 10000) {
                        YDUI.dialog.toast("短信发送成功",'success',1000);
                        SendTime("#send-code-id");
                    } else if (result.code == 20112) {
                        YDUI.dialog.toast('短信发送失败','error',1000);
                    } else if (result.code == 20000) {
                        YDUI.dialog.toast('请求失败','error',1000);
                    } else if (result.code == 29901) {
                        YDUI.dialog.toast('无效参数','error',1000);
                    } else {
                        YDUI.dialog.toast(result.message,"warning");
                    }

                }
            }
        );
    }
}

var wait = 60;//短信时间
function SendTime(parameter) {
    var Send = $("" + parameter + "");
    if (wait == 0) {
        Send.html("| 点击获取验证码");
        wait = 60;
        $("#send-code-id").removeAttr("disabled");
    } else {
        Send.html("重新发送(" + wait + ")s");
        wait--;
        setTimeout(function () {
            SendTime(parameter);
        }, 1000);
    }
}
